//
//  BoyeConfig.m
//  itboye2015002
//
//  Created by hebidu on 15/9/12.
//  Copyright (c) 2015年 Boye. All rights reserved.
//

#import "BoyeConfig.h"
#import "BoyeHttpClient.h"

@implementation BoyeConfig

+(NSString *)getDefaultBarTintColor{
    NSString * customer =  (NSString *)[self getValue:@"DEFAULT_BAR_TINT_COLOR"];
    if (customer == nil) {
        customer = @"#2F4F4F";
    }
    return customer;
}

+(NSString *) getUrlOfUserOrder:(NSInteger)uid :(NSString *)key :(NSString *)token{
    
    return [NSString stringWithFormat:@"%@Home/User/order?uid=%ld&key=%@&access_token=%@",BOYE_ROOT_URL,(long)uid,key,token];
    
}

+(NSString *) getUrlOfUserShare:(NSInteger)uid :(NSString *)key :(NSString *)token{
    
    return [NSString stringWithFormat:@"%@/Home/User/share?uid=%ld&key=%@&access_token=%@",BOYE_ROOT_URL,(long)uid,key,token];
}

+(NSString *) getUrlOfUserRebate:(NSInteger)uid :(NSString *)key :(NSString *)token{
    return [NSString stringWithFormat:@"%@Home/User/rebate?uid=%ld&key=%@&access_token=%@",BOYE_ROOT_URL,(long)uid,key,token];
}


/**
 *  帮助与反馈的web地址
 *
 *  @return 字符串
 */

+(NSString * )getUrlOfHelpAndFeedback{
    return [NSString stringWithFormat:@"%@Api/Feedback/index",BOYE_ROOT_URL];
}


+(NSString *) getUrlOfAboutBanma{
    return [NSString stringWithFormat:@"%@Public/html/about.html",BOYE_ROOT_URL];
}

+(NSString *) getUrlOfCopyright{
    return [NSString stringWithFormat:@"%@Public/html/copyright.html",BOYE_ROOT_URL];
}


+(NSString *) getCurrentVersion{
    
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    return app_Version;
}

+(NSString *) getCurrentBuild{
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app build版本
    NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
    
    return app_build;
}

//+(void)requestRemoteGlobalConfig{

//    if  ([CommonCache needReloadConfig] ){
//        
//        DLog(@"第一次请求服务器配置");
//        
//        [BoyeConfigHttpEngine requestAppConfig:^(NSDictionary * success){
//            
//            double interval = 3600*24*365;
//            for (NSDictionary * dict in success) {
//                
//                NSString *key = [dict valueForKey:@"name"];
//                NSString *value = [dict valueForKey:@"value"];
//                DLog(@"key=%@,value=%@",key,value);
//                [[CacheFacade sharedCache] setObject:value forKey:key afterSeconds:interval];
//                
//            }
//            
//        }];
//        
//    
//    }else{
//        
//        DLog(@"1. 博也缓存服务器配置 ! ");
//    }
    
//}

+ (void)requestRemoteGlobalConfig {
    
//    __block NSInteger refreshValue = 1;
    
    [self requestForceRefreshConfig:^(NSDictionary *success) {
        NSInteger refreshValue = [success[@"value"] integerValue];

        if ([CommonCache needReloadConfig] || refreshValue) {
            NSLog(@"第一次请求服务器配置!");
            
            [self requestAppConfig:^(NSDictionary *success) {
                double interval = 3600*24*365;
                NSLog(@"%@", success);
                for (NSDictionary *dic in success) {
                    
                    NSString *key = [dic valueForKey:@"name"];
                    NSString *value = [dic valueForKey:@"value"];
                    
                    [[CacheFacade sharedCache] setObject:value forKey:key afterSeconds:interval];
                }
            }];
            
        } else {
            NSLog(@"博也缓存服务器配置!");
        }

        
    }];
    
    
    
    
}

// 请求配置信息
+ (void)requestAppConfig:(void(^)(NSDictionary *))success {
    [BoyeHttpClient post:@"BY_Config_app" apiVerson:100 params:nil success:^(AFHTTPRequestOperation *peration, id result) {

        success(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"config_error:%@", error);
    }];
}

// 请求是否强制刷新
+ (void)requestForceRefreshConfig:(void(^)(NSDictionary *))success {
    
    [BoyeHttpClient post:@"BY_Config_force_refresh" apiVerson:100 params:nil success:^(AFHTTPRequestOperation *peration, id result) {
        // TODO: 先暂时本地设置为不强制刷新
//        result = @{@"value":@0};
        success(result);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"config_error:%@", error);
    }];
    
}


+(id) getValue:(NSString *)key  {
    return [[CacheFacade sharedCache]get:key];
}

+(NSNumber *) getDutyFreePrice{
    NSNumber * price =  (NSNumber *)[self getValue:@"DUTY_FREE_PRICE"];
    if (price == nil) {
        price = [NSNumber numberWithFloat:50.0f];
    }
    return price;
}

+(NSString *) getCustomePhone{
    
    NSString * customer =  (NSString *)[self getValue:@"CUSTOMER_PHONE"];
    if (customer == nil) {
        customer = @"";
    }
    return customer;
}

@end
